package com.demo.xmtx.enums;

import com.baomidou.mybatisplus.annotation.IEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum SexEnum implements IEnum<Integer> { // 枚举自动映射第二种方式，实现IEnum 接口
    MAN(0, "男"),
    WOMAN(1, "女");

    private final int code;
    private final String name;



    // 通过 code 获取枚举实例
    public static SexEnum fromCode(int code) {
        for (SexEnum type : SexEnum.values()) {
            if (type.getCode() == code) {
                return type;
            }
        }
        throw new IllegalArgumentException("未知的类型 : " + code);
    }

    // 通过 code 获取表名
    public static String getTableNameByCode(int code) {
        SexEnum type = fromCode(code);
        return type.getName();
    }

    @Override
    public Integer getValue() {
        return this.code;
    }
}
